package hk.edu.polyu.comp.motioncapture;

import hk.edu.polyu.comp.motioncapture.analysis.sequence.PointTrack;
import hk.edu.polyu.comp.motioncapture.point.*;
import hk.edu.polyu.comp.motioncapture.collection.DimensionGeometry;

;

public class Screen {
	private Point topLeftPt;
	private Point topRightPt;
	private Point bottomLeftPt;
	private Point bottomRightPt;

	public boolean getScreen(Camera camera) {
		boolean isSuccess = true;

		// Point sorting
		PointTrack
				.analyisiStaticDynamicPoints(camera.returnTimePointSets(1), 5);
		Frame vc = PointTrack.staticPointFrame;
		if (vc.size() != 4) {
			vc = DimensionGeometry.giftWrappingBound(vc);
		}
		// Point[] sortedPt = PointTrack.sortingPointY(vc);

		if (vc.size() == 4) {
			Point[] sortedPt = vc.getSortedPointsByY();
			// System.out.println(sortedPt.length);

			vc.removeAll();
			vc.add(sortedPt[0]);
			vc.add(sortedPt[1]);
			// Point[] sortedPtX1 = PointTrack.sortingPointX(vc);
			Point[] sortedPtX1 = vc.getSortedPointsByX();
			//System.out.println(sortedPtX1[0] + " " + sortedPtX1[1]);

			vc.removeAll();
			vc.add(sortedPt[2]);
			vc.add(sortedPt[3]);
			// Point[] sortedPtX2 = PointTrack.sortingPointX(vc);
			Point[] sortedPtX2 = vc.getSortedPointsByX();

			// Final
			Frame sortedPtFinal = new Frame(4, 1);
			sortedPtFinal.add(sortedPtX1[0]);
			sortedPtFinal.add(sortedPtX1[1]);
			sortedPtFinal.add(sortedPtX2[0]);
			sortedPtFinal.add(sortedPtX2[1]);

			topLeftPt = sortedPtX1[0];
			topRightPt = sortedPtX1[1];
			bottomLeftPt = sortedPtX2[0];
			bottomRightPt = sortedPtX2[1];

			System.out.println("" + topLeftPt + topRightPt + bottomLeftPt + bottomRightPt + "");
		} else {
			isSuccess = false;
		}

		return isSuccess;
	}

	public Point getTopLeftPoint() {
		return topLeftPt;
	}

	public Point getTopRightPoint() {
		return topRightPt;
	}

	public Point getBottomLeftPoint() {
		return bottomLeftPt;
	}

	public Point getBottomRightPoint() {
		return bottomRightPt;
	}

	public boolean isExist() {
		boolean isExist = false;

		if (topLeftPt != null && topRightPt != null && bottomLeftPt != null
				&& bottomRightPt != null) {
			isExist = true;
		}

		return isExist;
	}

	public String toString() {
		return ("TL:" + topLeftPt.toString() + " // " + "TR:"
				+ topRightPt.toString() + " //" + "BL:"
				+ bottomLeftPt.toString() + " // " + "BR:" + bottomRightPt
				.toString());
	}

	public double getWidth() {
		double max = Math.max(Math.max(topLeftPt.getX(), topRightPt.getX()),
				Math.max(bottomLeftPt.getX(), bottomRightPt.getX()));
		double min = Math.min(Math.min(topLeftPt.getX(), topRightPt.getX()),
				Math.min(bottomLeftPt.getX(), bottomRightPt.getX()));

		return (max - min);
	}

	public double getHeight() {
		double max = Math.max(Math.max(topLeftPt.getY(), topRightPt.getY()),
				Math.max(bottomLeftPt.getY(), bottomRightPt.getY()));
		double min = Math.min(Math.min(topLeftPt.getY(), topRightPt.getY()),
				Math.min(bottomLeftPt.getY(), bottomRightPt.getY()));

		return (max - min);
	}

	public Point transformation(Point opt) {
		
		double x = (opt.getX() - getTopLeftPoint().getX()) / getWidth()
				* 65535.0;
		double y = (opt.getY() - getTopLeftPoint().getY()) / getHeight()
				* 65535.0;
		
		System.out.println(getTopLeftPoint() + "  " + x + "  " + y);
		return (new Point(x, y));
	}
}
